# -*- coding: utf-8 -*-
###############################################################################
# file    : views_user_admin.py
# author  : wangyubin
# date    : Fri Apr 18 16:24:45 2014
# 
# brief   : views for 用户管理
# history : init
###############################################################################

# uliweb lib
from uliweb import expose, functions
# python lib
import os
import datetime
# CM-box lib
from db_app.model_define import *
from comm_app.my_log import App_Log
from comm_app import utils

@expose('/admin')
class views_user_admin(object):
    """ 用户管理
    """

    @expose('user-list/<search_name>')
    @expose('user-list', defaults={ "search_name": "" })
    def list(self, search_name):
        """ 用户列表
        """

        users = T_USER.filter(T_USER.c.name != 'admin')
        users = users.filter(T_USER.c.name.like('%' + search_name + '%') |
                             T_USER.c.real_name.like('%' + search_name + '%'))
        
        return {
            "users": users,
            "search_name": search_name,
        }


    @expose('del-user')
    def delete(self):
        """ 删除用户
        """

        user_id = request.POST.get("user_id")
        App_Log.logger.debug(u'user_id: ' + user_id)
        user = T_USER.get(int(user_id))
        if not user:            # 用户不存在, 可能已经被删除
            return json({
                "result": "OK",
                "message": settings.MESSAGE.ADMIN_MSG_02,
                "user_id": user_id,
            })

        # 用户有关联的BUG
        if (user.created_bugs and user.created_bugs.count() > 0) or \
           (user.assigned_bugs and user.assigned_bugs.count() > 0) or \
           (user.assigning_bugs and user.assigning_bugs.count() > 0):
            return json({
                "result": "NG",
                "message": settings.MESSAGE.ADMIN_MSG_03 % user.name,
            })

        username = user.name
        user.projects.clear()   # 清除和project的关系
        user.delete()           # 删除用户
        return json({
            "result": "OK",
            "message": settings.MESSAGE.ADMIN_MSG_04 % username,
            "user_id": user_id,
        })
        

    @expose('edit-user/<int:user_id>')
    def edit(self, user_id):
        """ 编辑或者新建用户
        """

        username = ""
        realname = ""
        email = ""
        telephone = ""

        user = T_USER.get(user_id)
        if user:
            username = user.name
            realname = user.real_name
            email = user.email
            telephone = user.telephone
        
        return {
            "user_id": user_id,
            "username": username,
            "realname": realname,
            "email": email,
            "telephone": telephone,
        }


    @expose('save-user')
    def save(self):
        """ 保存新注册或者修改的用户
        """
        user_id = request.POST.get("user_id")
        username = request.POST.get("username")
        realname = request.POST.get("realname")
        email = request.POST.get("email")
        telephone = request.POST.get("telephone")
        passwd = request.POST.get("passwd")

        App_Log.logger.debug(u'user_id: ' + user_id)
        App_Log.logger.debug(u'username: ' + username)
        App_Log.logger.debug(u'realname: ' + realname)
        App_Log.logger.debug(u'email: ' + email)
        App_Log.logger.debug(u'telephone: ' + telephone)
        App_Log.logger.debug(u'passwd: ' + passwd)

        user = T_USER.get(int(user_id))
        if user:
            user.real_name = realname
            user.email = email
            user.telephone = telephone
            if passwd:
                user.passwd = passwd

            user.save()
        else:
            # 检查用户名是否重复
            if T_USER.filter(T_USER.c.name == username).count() > 0:
                return json({
                    "result" : "NG",
                    "message": settings.MESSAGE.ADMIN_MSG_05 % username,
                })
            
            user = T_USER()
            user.name = username
            user.real_name = realname
            user.email = email
            user.telephone = telephone
            user.passwd = passwd
            user.save()

        return json({
            "result" : "OK",
            "message": "",
        })




